package com.test;

import com.boot.MainBoot;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = MainBoot.class)
public class SendMessage {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Test
    public void SendMessage1() {
        rabbitTemplate.convertAndSend("topicExchange", "com.sunjob.key1", "我是消息");
    }

    @Test
    public void SendMessage2() {
        rabbitTemplate.convertAndSend("topicExchange", "com.java", "我是消息");
    }

    @Test
    public void SendMessage3() {
        //判断交换机是否存在
        this.rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ask, String s) {
                if (ask) {
                    System.out.println("找到交换机");
                } else {
                    System.out.println("找不到交换机:" + s);
                }
            }
        });

        //判断队列是否存在
        this.rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                System.out.println("找不到队列");
            }
        });

        //消息持久化(交换机，队列，消息默认都是持久化)
        Message message = new Message("发送的内容".getBytes(),new MessageProperties());
        rabbitTemplate.convertAndSend("topicExchange2", "xxx.yyy", message);
    }

}
